// RC5.cpp : Defines the entry point for the console application.
//

/* RC5REF.C -- Reference implementation of RC5-32/12/16 in C.        */
/* Copyright (C) 1995 RSA Data Security, Inc.                        */
#include <stdio.h>
#include <time.h>
#include <string.h>
#include "rc5.h"
// typedef unsigned long int WORD; /* Should be 32-bit = 4 bytes        */
#define w        32             /* word size in bits                 */
#define r        12             /* number of rounds                  */  
#define b        16             /* number of bytes in key            */
#define c        (b*8/w)        /* number  words in key = ceil(8*b/w)*/
#define t        (2*(r+1))      /* size of table S = 2*(r+2) words   */
uint32_t S[t];                      /* expanded key table                */
uint32_t P = 0xb7e15163, Q = 0x9e3779b9;  /* magic constants             */
/* Rotation operators. x must be unsigned, to get logical right shift*/
#define ROTL(x,y) (((x)<<(y&(w-1))) | ((x)>>(w-(y&(w-1)))))
#define ROTR(x,y) (((x)>>(y&(w-1))) | ((x)<<(w-(y&(w-1)))))

void rc5_encrypt(uint32_t *pt, uint32_t *ct) /* 2 WORD input pt/output ct    */
{
	uint32_t i, A=pt[0]+S[0], B=pt[1]+S[1];
	for (i=1; i<=r; i++) 
	{
		A = ROTL(A^B,B)+S[2*i]; 
		B = ROTL(B^A,A)+S[2*i+1]; 
	}
	ct[0] = A; 
	ct[1] = B;  
} 

void rc5_decrypt(uint32_t *ct, uint32_t *pt) /* 2 WORD input ct/output pt    */
{
	uint32_t i, B=ct[1], A=ct[0];
	for (i=r; i>0; i--) 
	{ 
		B = ROTR(B-S[2*i+1],A)^A; 
		A = ROTR(A-S[2*i],B)^B; 
	}
	pt[1] = B-S[1]; 
	pt[0] = A-S[0];  
} 

void rc5_init(unsigned char *key) /* secret input key[0...b-1]      */
{  
	uint32_t i, j, k, u=w/8, A, B, L[c]; 
	/* Initialize L, then S, then mix key into S */
	for (i=b-1,L[c-1]=0; i!=-1; i--)
		L[i/u] = (L[i/u]<<8)+key[i];
	for (S[0]=P,i=1; i<t; i++) 
		S[i] = S[i-1]+Q;
	for (A=B=i=j=k=0; k<3*t; k++,i=(i+1)%t,j=(j+1)%c)   /* 3*t > 3*c */
	{ 
		A = S[i] = ROTL(S[i]+(A+B),3);  
		B = L[j] = ROTL(L[j]+(A+B),(A+B)); 
	} 
} 

void rc5_demo()	//rc5_demo
{ 
	uint32_t i, j, pt1[2], pt2[2], ct[2] = {0,0};
	unsigned char key[b];

	//demo 1
	printf("RC5-32/12/16 examples:\n");
	for (i=1;i<6;i++)
	{ /* Initialize pt1 and key pseudorandomly based on previous ct */
		pt1[0]=ct[0]; pt1[1]=ct[1]; 
		for (j=0;j<b;j++) key[j] = ct[0]%(255-j);
		/* Setup, encrypt, and decrypt */
		rc5_init(key);  
		rc5_encrypt(pt1,ct);  
		rc5_decrypt(ct,pt2);
		/* Print out results, checking for decryption failure */
		printf("\n%d. key = ",i); 
		for (j=0; j<b; j++) 
			printf("%.2X ",key[j]);

		printf("\n   plaintext %.8lX %.8lX  --->  ciphertext %.8lX %.8lX  \n",
			pt1[0], pt1[1], ct[0], ct[1]);

		if (pt1[0]!=pt2[0] || pt1[1]!=pt2[1]) 
			printf("Decryption Error!");
	}

	//demo 2
	printf("\nrc5 demo 2\n");
	memcpy(key,"1234567890ABCDEF",16);
	unsigned char plaintext[8],ciphertext[8];
	memcpy(plaintext,"qwertyui",8);

	rc5_init(key);  
	rc5_encrypt((uint32_t*)plaintext, (uint32_t*)ciphertext);
	printf("ciphertext: ");
	for(i=0;i<8;i++) printf("%02x",ciphertext[i]);
	printf("\n");
	rc5_decrypt((uint32_t*)ciphertext, (uint32_t*)plaintext);
	printf("plaintext: ");
	for(i=0;i<8;i++) printf("%02x",plaintext[i]);
	printf("\n");
	printf("plaintext: %.*s\n",8,plaintext);

}